package com.appkubes.portal.admin.application.app.push.controller;

import com.appkubes.base.core.entity.vo.PageVO;
import com.appkubes.common.Response;
import com.appkubes.portal.admin.application.app.push.entity.TransmissionContent;
import com.appkubes.portal.admin.application.app.push.entity.vo.PushVO;
import com.appkubes.portal.admin.application.app.push.util.PushUtil;
import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gexin.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@Api(tags = "推送模块")
@RestController
@RequestMapping("/app/push")
public class PushController {

    @Resource
    BasePortalAppPushService pushService;

    @ApiOperation(("添加推送"))
    @PostMapping("/add")
    Response add(@ParamValid PushVO pushVO) {
        TransmissionContent transmissionContent = new TransmissionContent();
        transmissionContent.setTitle(pushVO.getTitle());
        transmissionContent.setContent(pushVO.getContent());
        transmissionContent.setType(pushVO.getType());
        transmissionContent.setParam(pushVO.getParam());
        String res = "";
        PortalAppPush appPush=new PortalAppPush();
        appPush.setClientId(pushVO.getCid());
        appPush.setPushTitle(pushVO.getTitle());
        appPush.setContent(pushVO.getContent());
        appPush.setTransmissionInfo(JSONObject.toJSONString(transmissionContent));
        if (pushVO.getCid().equals("")) {
            appPush.setPushType("GLOBAL");
            res = PushUtil.allPush(transmissionContent);
        } else {
            appPush.setPushType("SINGLE");
            res = PushUtil.singlePush(pushVO.getCid(), transmissionContent);
        }
        if(res.equals("ok")){
            pushService.save(appPush);
            return Response.success(res);
        }else{
            return Response.error(res);
        }
    }

    @ApiOperation("获推送列表")
    @GetMapping("/get")
    public Response get(@ParamValid PageVO pageVO) {
        IPage<?> pageResult = pushService.page(new Page(pageVO.getPage(),pageVO.getSize()));
        return Response.success(pageResult);
    }
}
